<?php
$param = file_get_contents('php://input');
// $param = file_get_contents('notify1.txt');
file_put_contents('pre_order_refund.txt', $param);
$param = json_decode($param, true);
$url = file_get_contents('notify_url.txt');
if (!empty($param['msg'])) {
    $out_order_no = 'LD' . time() . uniqid();
    $msg = $param['msg'];
    $msg = json_decode($msg, true);
    $cp_extra = $msg['cp_extra'];
    // var_dump($msg['goods'][0]['goods_id']);
    // exit;
    send_post($url, arrayToXml([
        "status" => "order",
        "out_order_no" => $out_order_no,
        "cp_extra" => $cp_extra
    ]));

    echo json_encode([
        'err_no' => 0,
        'err_tips' => 'success',
        'data' => [
            "out_order_no" => $out_order_no,//随机字符串函数 
            "pay_expire_seconds" => 300,
            "order_entry_schema" => [
                // "path" => "ext://microapp-trade-plugin/order-confirm" //订单详情页跳转路径
                "path" => "pages/user/index" //订单详情页跳转路径
            ],
            "order_goods_info" => [
                [
                    "goods_id" => $msg['goods'][0]['goods_id'],
                    // "merchant_uid" => "73686830333328448340"
                ]
            ]
            // "pay_notify_url" => ""  //支付回调
        ],

    ]);
} else {
    echo json_encode([
        'err_no' => 1,
        'err_tips' => 'success',

    ]);
}



exit;
function arrayToXml($arr)
{

    $xml = "<xml>";
    foreach ($arr as $key => $val) {
        if (is_numeric($val)) {
            $xml .= "<$key>$val</$key>";
        } else
            $xml .= "<$key><![CDATA[$val]]></$key>";
    }
    $xml .= "</xml>";
    return $xml;
}
function send_post($url, $post_data)
{
    // $postdata = http_build_query($post_data);
    file_put_contents('xml.txt', $post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/xml',
            'content' => $post_data,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}